package com.dswz.controller.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dswz.po.Catalog;
import com.dswz.service.CatalogService;
import com.dswz.service.SessionService;
import com.dswz.utils.AdminResult;
import com.dswz.utils.RequestUtils;
import com.dswz.utils.Utils;
import com.google.gson.Gson;


@Controller
@RequestMapping({ "/admins" })
public class CatalogAdminController {
	private Logger logger = LogManager.getLogger(CatalogAdminController.class);

	@Autowired
	private CatalogService catalogService;

	@Autowired
	private SessionService sessionService;

	@RequestMapping(value = { "/getCatalogList" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public Object getCatalogList(Model model, HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"ADMIN_SESSION");
			this.logger.info("客户端:" + request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request)
					+ " 操作人: " + username);
			List<Catalog> list = this.catalogService.getCatalogList();
			result.setResult(0);
			result.setMessage("OK");
			result.setRows(list);
			result.setTotal(list.size());
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			result.setResult(2);
			result.setMessage(e.getMessage());
			this.logger.error(e.getMessage(), e);
		}
		return result;
	}

	@RequestMapping(value = { "/editCatalog" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	@ResponseBody
	public Object editGoods(Integer id, Model model, HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"ADMIN_SESSION");
			this.logger.info("CatalogId:" + id + "页面来源:" + request.getHeader("referer") + "客户端:"
					+ request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: "
					+ username);
			Catalog catalog = this.catalogService.getCatalogById(id);
			result.setData(catalog);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}

	@RequestMapping(value = { "/saveEditCatalog" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public Object saveEditCatalog(Integer id, String name, Integer order, Integer pid, Model model,
			HttpServletRequest request, HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"ADMIN_SESSION");
			this.logger.info("页面来源:" + request.getHeader("referer") + "客户端:" + request.getHeader("user-agent")
					+ " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: " + username);
			if (StringUtils.isEmpty(id.toString())) {
				this.logger.error("id为空!");
				result.setMessage("id为空!");
				result.setResult(3);
				return result;
			}
			if (StringUtils.isEmpty(name)) {
				this.logger.error("请输入目录名称!");
				result.setMessage("请输入目录名称!");
				result.setResult(3);
				return result;
			}

			Catalog catalog = new Catalog();
			catalog.setId(id);
			catalog.setName(name);
			catalog.setPid(pid);
			catalog.setOrder(order);
			this.catalogService.updateCatalog(catalog);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}

	@RequestMapping(value = { "/saveAddCatalog" }, method = {
			org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public Object saveAddCatalog(String name, Integer order, Integer pid, Model model, HttpServletRequest request,
			HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"ADMIN_SESSION");
			this.logger.info("页面来源:" + request.getHeader("referer") + "客户端:" + request.getHeader("user-agent")
					+ " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: " + username);
			if (StringUtils.isEmpty(name)) {
				this.logger.error("请输入目录名称!");
				result.setMessage("请输入目录名称!");
				result.setResult(3);
				return result;
			}
			Catalog catalog = new Catalog();
			catalog.setName(name);
			catalog.setPid(pid);
			catalog.setOrder(order);
			this.catalogService.insertCatalog(catalog);
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}

	@RequestMapping({ "/deleteCatalog" })
	@ResponseBody
	public Object deleteCatalog(String catalogIds, Model model, HttpServletRequest request,
			HttpServletResponse response) {
		long startTime = System.currentTimeMillis();
		AdminResult result = new AdminResult();
		try {
			String username = this.sessionService.getAttibute(RequestUtils.getCSESSIONID(request, response),
					"ADMIN_SESSION");
			this.logger.info("catalogIds:" + catalogIds + "页面来源:" + request.getHeader("referer") + "客户端:"
					+ request.getHeader("user-agent") + " 客户端ip: " + Utils.getUserRealAddress(request) + " 操作人: "
					+ username);
			List<String> list = new ArrayList();
			if (StringUtils.isEmpty(catalogIds)) {
				this.logger.error("请选择一个");
				result.setMessage("请选择一个");
				result.setResult(3);
				return result;
			}
			String[] catalogIdArr = catalogIds.split(",");
			String[] arrayOfString1;
			int j = (arrayOfString1 = catalogIdArr).length;
			for (int i = 0; i < j; i++) {
				String catalogId = arrayOfString1[i];
				list.add(catalogId);
			}
			this.catalogService.deleteCatalog(Integer.parseInt(catalogIds));
			result.setResult(0);
			result.setMessage("OK");
			long endTime = System.currentTimeMillis();
			this.logger.info("result:" + new Gson().toJson(result) + "程序运行时间" + (endTime - startTime) + "ms");
		} catch (Exception e) {
			this.logger.error(e.getMessage(), e);
			result.setMessage(e.getMessage());
			result.setResult(2);
		}
		return result;
	}
}
